package com.llt.blog.config;

import com.llt.blog.constants.Constant;
import com.llt.blog.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1. 从 head 中获取token
        String userTokenHeader = request.getHeader(Constant.USER_TOKEN_HEADER);

        boolean result = JwtUtils.checkToken(userTokenHeader);
        if (result){
            return true;
        }

        response.setStatus(401);
        return false;

    }
}
